package com.admin.sys.base.websocket.service;

import com.admin.entity.Register;
import com.admin.service.RegisterService;
import com.admin.entity.SysUser;
import com.admin.service.SysUserService;
import com.admin.sys.base.websocket.entity.SessionPool;
import com.admin.sys.base.websocket.entity.SessionUser;
import com.admin.sys.base.module.extend.service.BaseService;
import com.admin.sys.base.module.message.service.SysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.websocket.Session;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//注册信息接口类
@Service
public class SessionService extends BaseService {

    @Autowired
    private RegisterService registerService;
    @Autowired
    private SysUserService sysUserService;

    private SysMessageService sysMessageService;
    //设置用户在线
    Map<String, Session> sessionMap = SessionPool.sessionMap;

    /**
     * 获取前台用户
     * @return
     */
    public List<SessionUser> getFrontList() {
        List<SessionUser> userList = new ArrayList<>();
        List<Register> registerList = registerService.getList(null);
        for (Register register : registerList) {
            SessionUser sessionUser = new SessionUser();
            sessionUser.setId(register.getId());
            sessionUser.setLoginName(register.getLoginName());
            sessionUser.setPhoto(register.getPhoto());
            sessionUser.setSex(register.getSex());
            sessionUser.setUserName(register.getUserName());
            userList.add(sessionUser);
        }
        for (Map.Entry<String, Session> entry : sessionMap.entrySet()) {
            String onlineUserId = entry.getKey();
            for (SessionUser sessionUser : userList) {
                if (onlineUserId.equals(sessionUser.getId())) {
                    sessionUser.setOnline(true);
                }
            }
        }
        return userList;
    }

    /**
     * 获取后台用户
     * @return
     */
    public List<SessionUser> getAdminList(String roleCode) {
        List<SessionUser> userList = new ArrayList<>();
        SysUser params = new SysUser();
        params.setRoleCode(roleCode);
        List<SysUser> sysUserList = sysUserService.getList(params);
        for (SysUser sysUser : sysUserList) {
            if(!"dev".equals(sysUser.getLoginName())){
                SessionUser sessionUser = new SessionUser();
                sessionUser.setId(sysUser.getId());
                sessionUser.setLoginName(sysUser.getLoginName());
                sessionUser.setPhoto(sysUser.getPhoto());
                sessionUser.setSex(sysUser.getSex());
                sessionUser.setUserName(sysUser.getUserName());
                userList.add(sessionUser);
            }
        }
        //设置用户在线
        for (Map.Entry<String, Session> entry : sessionMap.entrySet()) {
            String onlineUserId = entry.getKey();
            for (SessionUser sessionUser : userList) {
                if (onlineUserId.equals(sessionUser.getId())) {
                    sessionUser.setOnline(true);
                }
            }
        }
        return userList;
    }

    /**
     * 获取所有用户
     * @return
     */
    public List<SessionUser> getAllList() {
        List<SessionUser> userList = new ArrayList<>();
        List<Register> registerList = registerService.getList(null);
        for (Register register : registerList) {
            SessionUser sessionUser = new SessionUser();
            sessionUser.setId(register.getId());
            sessionUser.setLoginName(register.getLoginName());
            sessionUser.setPhoto(register.getPhoto());
            sessionUser.setSex(register.getSex());
            sessionUser.setUserName(register.getUserName());
            userList.add(sessionUser);
        }
        List<SysUser> sysUserList = sysUserService.getList(new SysUser());
        for (SysUser sysUser : sysUserList) {
            if(!"dev".equals(sysUser.getLoginName())){
                SessionUser sessionUser = new SessionUser();
                sessionUser.setId(sysUser.getId());
                sessionUser.setLoginName(sysUser.getLoginName());
                sessionUser.setPhoto(sysUser.getPhoto());
                sessionUser.setSex(sysUser.getSex());
                sessionUser.setUserName(sysUser.getUserName());
                userList.add(sessionUser);
            }
        }
        //设置用户在线
        for (Map.Entry<String, Session> entry : sessionMap.entrySet()) {
            String onlineUserId = entry.getKey();
            for (SessionUser sessionUser : userList) {
                if (onlineUserId.equals(sessionUser.getId())) {
                    sessionUser.setOnline(true);
                }
            }
        }
        return userList;
    }

}